package hc.bre

import static org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test

import hc.bre.variable.VariableDefinition
import hc.bre.variable.VariableItem

@groovy.transform.CompileStatic
class VariableTest {

	@Test
	void create() {
		VariableItem varItem = new VariableItem();
		VariableDefinition ruleDef = new VariableDefinition();
		assert ruleDef.rtype =='variable'
	}
	String jsonText ='''
{
  "rid": 263,
  "rtype": "variable",
  "rname": null,
  "rpath": "xsxdjc/Variable",
  "rversion": 6,
  "rmemo": "",
  "rtest": "{}",
  "rmaxid": 14,
  "list": [
    {
      "id": 1,
      "code": "基本信息",
      "name": "",
      "type": "object",
      "list": [
        {
          "id": 2,
          "code": "姓名",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 4,
          "code": "年龄",
          "name": "",
          "type": "number",
          "list": null
        },
        {
          "id": 5,
          "code": "性别",
          "name": "1:女，2：男，其它：未知",
          "type": "number",
          "list": null
        },
        {
          "id": 6,
          "code": "学历",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 7,
          "code": "专业",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 8,
          "code": "省市区",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 9,
          "code": "详细住址",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 10,
          "code": "身份证号",
          "name": "",
          "type": "text",
          "list": null
        },
        {
          "id": 11,
          "code": "联系手机",
          "name": "手机号码很重要",
          "type": "number",
          "list": null
        }
      ]
    },
    {
      "id": 12,
      "code": "进件信息",
      "name": "",
      "type": "object",
      "list": [
        {
          "id": 13,
          "code": "合同编码",
          "name": "contract",
          "type": "text",
          "list": null
        },
        {
          "id": 14,
          "code": "合同名称",
          "name": "contract name",
          "type": "text",
          "list": null
        }
      ]
    }
  ]
}
'''
	@Test
	void load() {
		VariableDefinition ruleDef = new VariableDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));
		assert ruleDef.list[1].list[1]['id'] == 14 ;
		assert ruleDef.list[1].list[1]['code'] == "合同名称" ;
		assert ruleDef.list[1].list[1]['name'] == "contract name" ;
		assert ruleDef.list[1].list[1]['type'] == "text" ;
	}


	@Test
	void route() {
		VariableDefinition ruleDef = new VariableDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));

		assert ruleDef.accessRouteOf(12) == '进件信息'
		assert ruleDef.accessRouteOf(14) == '进件信息.合同名称'
		assert ruleDef.accessRouteOf(0) == ''
	}
	
	
	@Test
	void buildScript_Empty() {
		VariableDefinition ruleDef = new VariableDefinition();
		ruleDef.load(hc.Util.jsonRead(jsonText));
		String script = ruleDef.buildRuleScript();
		assert script == '';
//		Class clazz = ruleDef.buildRuleClass();
//		println script;
	}
}
